﻿using System;
using System.Collections.Generic;
using System.Linq;

using Common;
using DAL;
using BLL;
using System.Web.Mvc;
using System.Text;
using System.EnterpriseServices;
using System.Configuration;
using App.Models;
using App.Services;
using App.Models.Pager;
namespace App.Controllers
{
    /// <summary>
    /// 模块
    /// </summary>
    public class SysMenuController : BaseController
    {
        /// <summary>
        /// Flexigrid异步加载数据
        /// </summary>
        /// <param name="param">Flexigrid传过到后台的参数</param>
        /// <returns></returns>
        [HttpPost]
        [SupportFilter]
        public JsonResult FlexigridList(string id, FlexigridParam param)
        {
            return Json(m_BLL.FlexigridList(id, param));
        }
        /// <summary>
        /// 在查询中输入字符串，自动提示的功能
        /// </summary>
        /// <param name="id">需要查询的数据库字段的名称</param>
        /// <param name="term">输入的字符串</param>
        /// <returns></returns> 
        public ActionResult SearchAutoComplete(string id, string term)
        {
            return new ContentResult() { Content = m_BLL.SearchAutoComplete(id, term) };
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [SupportFilter]
        public ActionResult Index()
        {
        
            return View();
        }
        /// <summary>
        /// 查看详细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [SupportFilter]  
        public ActionResult Details(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                var item = m_BLL.GetById(id);
                if (item != null)
                {
                    return View(item);
                }
            }
            return RedirectToAction("Index", "NotFound");
        }
        /// <summary>
        /// 首次创建
        /// </summary>
        /// <returns></returns>
        [SupportFilter]
        public ActionResult Create(string id)
        { 
            
            return View();
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [SupportFilter]
        public ActionResult Create(SysMenu entity)
        {           
            if (entity != null && ModelState.IsValid)
            {
                entity.CreatePerson = GetCreatePerson();
                entity.Id = Guid.NewGuid().ToString();   
                
                if (m_BLL.Create(ref validationErrors, entity))
                {
                    log.WriteServiceLog(LogType.Operation, Suggestion.InsertSucceed  + "，模块的信息的Id为" + entity.Id,"模块",
                        Result.Succeed, ConfigurationManager.AppSettings["ServiceId"]);//写入日志 
                    return Json(Suggestion.InsertSucceed);
                }
            }
            log.WriteServiceLog(LogType.Operation, Suggestion.InsertFail + "，模块的信息的Id为" + entity.Id,"模块", 
                Result.Fail, ConfigurationManager.AppSettings["ServiceId"]);//写入日志
            return Json(Suggestion.InsertFail);
        }
        /// <summary>
        /// 首次编辑
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns> 
        [SupportFilter] 
        public ActionResult Edit(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                var entity = m_BLL.GetById(id);
                if (entity != null)
                {                      
                    return View(entity);
                }
            }
            return RedirectToAction("Index", "NotFound");
        }
        /// <summary>
        /// 提交编辑信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="collection">客户端传回的集合</param>
        /// <returns></returns>
        [HttpPost]
        [SupportFilter]
        public ActionResult Edit(string id, SysMenu entity)
        {
            if (!string.IsNullOrEmpty(id) && entity != null)
            {              
              
                if (ModelState.IsValid)
                {//数据校验   
                    entity.UpdatePerson = GetCreatePerson();
                    entity.UpdateTime = DateTime.Now;
                    if (m_BLL.Edit(ref validationErrors, entity))
                    {
                        log.WriteServiceLog(LogType.Operation, Suggestion.UpdateSucceed + "，模块信息的Id为" + id,"模块",
                            Result.Succeed, ConfigurationManager.AppSettings["ServiceId"]);//写入日志                           
                        return Json(Suggestion.UpdateSucceed); //提示更新成功 
                    }
                    else
                    { 
                        log.WriteServiceLog(LogType.Operation, Suggestion.UpdateFail + "，模块信息的Id为" + id,"模块", 
                            Result.Fail, ConfigurationManager.AppSettings["ServiceId"]);//写入日志                           
                        return Json(Suggestion.UpdateFail); //提示更新失败
                    }
                }
                else
                {
                    return Json(Suggestion.UpdateFail + "，请核对输入的数据的格式"); //提示输入的数据的格式不对               
                }
            }
            return RedirectToAction("Index", "NotFound");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>   
        [HttpPost]
        [SupportFilter]
        public ActionResult Delete(FormCollection collection)
        {
            string[] deleteId = collection["query"].GetString().Split(',');
            if (deleteId != null && deleteId.Length > 0)
            {
                if (m_BLL.DeleteCollection(ref validationErrors, deleteId))
                {
                    log.WriteServiceLog(LogType.Operation, Suggestion.DeleteSucceed + "，信息的Id为" + string.Join(",", deleteId), "消息",
                        Result.Succeed, ConfigurationManager.AppSettings["ServiceId"]);//删除成功，写入日志
                    return Json("OK");
                }
                else
                {
                    log.WriteServiceLog(LogType.Operation, Suggestion.DeleteFail + "，信息的Id为" + string.Join(",", deleteId), "消息",
                        Result.Fail, ConfigurationManager.AppSettings["ServiceId"]);//删除失败，写入日志
                }
            }
            return Json(string.Empty);
        }

        SysMenuBLL m_BLL;
        ValidationErrors validationErrors = new ValidationErrors();
        public SysMenuController()
            : this(new SysMenuBLL()) { }

        public SysMenuController(SysMenuBLL bll)
        {
            m_BLL = bll;
        }
      
    }
}

